import pymysql
from pymysql.cursors import DictCursor

CONFIG = {
    "host": "39.107.245.102",
    "port": 3306,
    "user": "root",
    "password": "wuji5201314.",
    "db": "school",
    "charset": "utf8",
    "cursorclass": DictCursor
}

class DB():
    def __init__(self):
        # 数据库的链接
        self.con = pymysql.Connect(**CONFIG)

    def __enter__(self):
        # 进入上下文的方法
        return self.con.cursor()

    def __exit__(self, exc_type, exc_val, exc_tb):
        # 退出上下文的方法
        if exc_type:
            self.con.rollback()
        else:
            self.con.commit()

    def close(self):
        if self.con:
            self.con.close()
            self.con = None

class BaseDao():
    def __init__(self):
        self.db = DB()
    def find_all(self,table,where=None,*whereArgs):
        sql = "select * from %s " % table
        if where:
            sql += where
        with self.db as c:
            c.execute(sql,whereArgs)
            result = list(c.fetchall())
        return result
